package com.xiashitech.agent.utils;

import com.xiashitech.agent.config.StartConfig;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class LocalMachineUtil {
    private static List<String> ipv4List = new ArrayList();
    private static List<String> ipv6List = new ArrayList();
    private static List<String> ipv4HostNameList = new ArrayList();
    private static List<String> ipv6HostNameList = new ArrayList();
    private static AtomicBoolean isInit = new AtomicBoolean(false);

    /**
     * 获取本机所有ipv4地址
     * @return
     */
    public static List<String> getlocalIpv4s() {
        return ipv4List;
    }
    /**
     * 获取本机所有ipv6地址
     * @return
     */
    public static List<String> getlocalIpv6s() {
        return ipv6List;
    }

    /**
     * 获取所有ipv4地址对应的hostname
     * @return
     */
    public static List<String> getIpv4HostNames() {
        return ipv4HostNameList;
    }
    /**
     * 获取所有ipv6地址对应的hostname
     * @return
     */
    public static List<String> getIpv6HostNames() {
        return ipv6HostNameList;
    }

    /**
     * 获取操作系统名称
     * @return
     */
    public static String getOsName() {
        String osName = System.getProperty("os.name");
        if (osName != null && !"".equals(osName)) {
            return osName;
        } else {
            return "";
        }
    }

    /**
     * 获取操作系统构架
     * @return
     */
    public static String getOsArch() {
        String osArch = System.getProperty("os.arch");
        if (osArch != null && !"".equals(osArch)) {
            return osArch;
        } else {
            return "";
        }
    }

    /**
     * 获取操作系统版本
     * @return
     */
    public static String getOsVersion() {
        String osVersion = System.getProperty("os.version");
        if (osVersion != null && !"".equals(osVersion)) {
            return osVersion;
        } else {
            return "";
        }
    }

    /**
     * 获取服务器所在的国家码
     * @return
     */
    public static String getUserCountry() {
        String userCountry = System.getProperty("user.country");
        if (userCountry != null && !"".equals(userCountry)) {
            return userCountry;
        } else {
            return "";
        }
    }

    /**
     * 获取服务器主机名
     * @return
     */
    public static String getMachineName() {
        String userName = System.getProperty("user.name");
        if (userName != null && !"".equals(userName)) {
            return userName;
        } else {
            return "";
        }
    }

    /**
     * 获取程序使用的国际化语言
     * @return
     */
    public static String getUserLanguage() {
        String userLanguage = System.getProperty("user.language");
        if (userLanguage != null && !"".equals(userLanguage)) {
            return userLanguage;
        } else {
            return "";
        }
    }

    /**
     * 获取系统的当前时区
     * @return
     */
    public static String getUserTimezone() {
        String userTimezone = System.getProperty("user.timezone");
        if (userTimezone != null && !"".equals(userTimezone)) {
            return userTimezone;
        } else {
            return "";
        }
    }

    public static void init() {
        if(!isInit.compareAndSet(false,true)) {
            return;
        }
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                if (!networkInterface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if(inetAddress.isLoopbackAddress()) {
                        continue;
                    }
                    if (inetAddress instanceof Inet4Address) {
                        ipv4List.add(inetAddress.getHostAddress());
                        ipv4HostNameList.add(inetAddress.getHostName());
                    } else if(inetAddress instanceof Inet6Address) {
                        ipv6List.add(inetAddress.getHostAddress());
                        ipv6HostNameList.add(inetAddress.getHostName());
                    }
                }
            }
        } catch (Throwable e) {
            StartConfig.throwableStackPrinter(e);
        }
    }
}
